package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frejoys.common.dto.admin.StoreListDto;
import com.frejoys.common.dto.app.CategoryIdDto;
import com.frejoys.common.vo.admin.StoreVo;
import com.frejoys.common.vo.agent.AgentStoreVo;
import com.frejoys.common.vo.app.StoreListInfoVo;
import com.frejoys.dao.entity.Store;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface StoreMapper extends BaseMapper<Store> {

    /************ 平台 ************/

    @Select("select * from j_store")
    // @Select("select * from j_store where id=1")
    List<Store> listInfo(Page<Store> page);

    // 获取店铺上架状态
    @Select("select shelves from j_store where id=#{id} limit 1")
    Store getShelvesInfo(@Param("id") Integer id);

    // 更新店铺上架状态
    @Update("update j_store set shelves=#{shelves} where id=#{id}")
    Boolean upShelvesInfo(@Param("id") Integer id, @Param("shelves") Integer shelves);

    @Select("select * from j_store where shelves = 2 and `status` = 1 and 6371 * acos(cos(radians(#{lat})) * cos(radians(lat)) * cos(radians(lng) - radians(#{lng})) + sin(radians(#{lat})) * sin(radians(lat))) <= 5")
    List<Store> selectNearby(@Param("page") Page<Store> page,@Param("lng") Double lng, @Param("lat") Double lat);

    List<Store> getStoreList(List<Integer> storeIds);

    List<AgentStoreVo> getStoreInfoList(List<Integer> storeIds);

    @Select(" SELECT name,id,logo,category_id,cover,cover_show,tel,province,city,district,street,address  ,type,shelves,start_time,end_time FROM j_store where id = #{storeId}")
    AgentStoreVo getStoreInfo(@Param("storeId") Integer storeId);

    List<Store> getStoreListByActivityId(List<Integer> activityIds);

    // 获取店铺名称和logo
    @Select("select name,logo from j_store where id=#{id} limit 1")
    Store getNameInfo(@Param("id") Integer id);

    // 获取店铺简单信息
    @Select("select name,logo,cover from j_store where id=#{id}")
    Store getSimpleInfo(@Param("id") Integer id);

    // 获取店铺id 状态信息
    @Select("select id,status from j_store where id=#{id}")
    Store getInfo(@Param("id") Integer id);

    //获取店铺名字和logo和电话
    List<Store> getStoreNameList(@Param("storeIds") List<Integer> storeIds);

    //查询店铺列表 根据距离排序
    List<StoreListInfoVo> categoryIdStoreList(@Param("page") Page page,@Param("dto") CategoryIdDto dto);

    // 更新售出数量
    @Update("update j_store set sold_count=sold_count+#{soldCount} where id=#{id}")
    Boolean upSoldCount(@Param("id") Integer id, @Param("soldCount") Integer soldCount);

    //管理员查询店铺列表
    IPage<StoreVo> getStoreListInfo(@Param("page") Page<Store> page,@Param("dto") StoreListDto dto,@Param("storeIds") List<Integer> storeIds);

    //根据店铺名字模糊查询id
    @Select("select id from j_store where `name` like concat('%',#{storeName},'%')")
    List<Store> getStoreNameById(@Param("storeName") String storeName);

    //根据电话查询店铺id
    @Select("select id from j_store where tel = #{tel}")
    List<Integer> telByStoreId(@Param("tel") String tel);
}
